file (GLOB PO_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.po")
include (CMakeDependentOption)

function (SetTranslationOption PoName DefaultMode)
	set (LangName "${PoName}")
	string (REGEX REPLACE "^ar$"	"Arabic"		LangName "${LangName}")
	string (REGEX REPLACE "^ast$"	"Asturian"		LangName "${LangName}")
	string (REGEX REPLACE "^bg$"	"Bulgarian"		LangName "${LangName}")
	string (REGEX REPLACE "^ca$"	"Catalan"		LangName "${LangName}")
	string (REGEX REPLACE "^cs$"	"Czech"			LangName "${LangName}")
	string (REGEX REPLACE "^da$"	"Danish"		LangName "${LangName}")
	string (REGEX REPLACE "^de$"	"German"		LangName "${LangName}")
	string (REGEX REPLACE "^el$"	"Greek"			LangName "${LangName}")
	string (REGEX REPLACE "^en_GB$"	"English"		LangName "${LangName}")
	string (REGEX REPLACE "^es$"	"Spanish"		LangName "${LangName}")
	string (REGEX REPLACE "^et_EE$"	"Estonian"		LangName "${LangName}")
	string (REGEX REPLACE "^eu$"	"Basque"		LangName "${LangName}")
	string (REGEX REPLACE "^fi$"	"Finnish"		LangName "${LangName}")
	string (REGEX REPLACE "^fr$"	"French"		LangName "${LangName}")
	string (REGEX REPLACE "^gl$"	"Galician"		LangName "${LangName}")
	string (REGEX REPLACE "^he$"	"Hebrew"		LangName "${LangName}")
	string (REGEX REPLACE "^hr$"	"Croatian"		LangName "${LangName}")
	string (REGEX REPLACE "^hu$"	"Hungarian"		LangName "${LangName}")
	string (REGEX REPLACE "^it$"	"Italian"		LangName "${LangName}")
	string (REGEX REPLACE "^it_CH$"	"Italian (Swiss)"	LangName "${LangName}")
	string (REGEX REPLACE "^ja$"	"Japanese"		LangName "${LangName}")
	string (REGEX REPLACE "^ko_KR$"	"Korean"		LangName "${LangName}")
	string (REGEX REPLACE "^lt$"	"Lithuanian"		LangName "${LangName}")
	string (REGEX REPLACE "^nl$"	"Dutch"			LangName "${LangName}")
	string (REGEX REPLACE "^nn$"	"Norwegian (Nynorsk)"	LangName "${LangName}")
	string (REGEX REPLACE "^pl$"	"Polish"		LangName "${LangName}")
	string (REGEX REPLACE "^pt_BR$"	"Portuguese (Brazilian)" LangName "${LangName}")
	string (REGEX REPLACE "^pt_PT$"	"Portuguese"		LangName "${LangName}")
	string (REGEX REPLACE "^ro$"	"Romanian"		LangName "${LangName}")
	string (REGEX REPLACE "^ru$"	"Russian"		LangName "${LangName}")
	string (REGEX REPLACE "^sl$"	"Slovenian"		LangName "${LangName}")
	string (REGEX REPLACE "^sq$"	"Albanian"		LangName "${LangName}")
	string (REGEX REPLACE "^sv$"	"Swedish"		LangName "${LangName}")
	string (REGEX REPLACE "^tr$"	"Turkish"		LangName "${LangName}")
	string (REGEX REPLACE "^uk$"	"Ukrainian"		LangName "${LangName}")
	string (REGEX REPLACE "^zh_CN$"	"Chinese (Simplified)"	LangName "${LangName}")
	string (REGEX REPLACE "^zh_TW$"	"Chinese (Traditional)"	LangName "${LangName}")
	cmake_dependent_option (TRANSLATION_${PoName} "Build ${LangName} translation" ${DefaultMode} ENABLE_NLS OFF)
endfunction()

foreach (PO_FILE ${PO_FILES})
	string (REGEX REPLACE ".po" "" PO_FILE ${PO_FILE})

	if (TRANSLATIONS)
		string (REGEX MATCH "${PO_FILE}," TRANS "${TRANSLATIONS}")

		if (TRANS)
			SetTranslationOption ("${PO_FILE}" ON)
		else()
			SetTranslationOption ("${PO_FILE}" OFF)
		endif()
	else()
		SetTranslationOption ("${PO_FILE}" ON)
	endif()

	if (TRANSLATION_${PO_FILE})
		list (APPEND PO_BUILD ${PO_FILE}.po)

		install (FILES ${CMAKE_CURRENT_BINARY_DIR}/${PO_FILE}.gmo
			DESTINATION ${CMAKE_INSTALL_LOCALEDIR}/${PO_FILE}/LC_MESSAGES/
			RENAME amule.mo
		)
	endif()
endforeach()

gettext_process_po_files ("" ALL PO_FILES ${PO_BUILD})
